package org.jftstool.auxiliar;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.jftstool.analyzers.JFTSAnalyzer;
import org.jftstool.analyzers.SpanishAnalyzer;
import org.jftstool.analyzers.exceptions.AnalyzerException;

public class BuildSynonymIndex {
	
	public static void main(String[] args) {
		File f = new File("c:\\sinonimos.dat");
		InputStream is = null;
		int cantidad = 0;
		String aux = null;
		List<String> synonymous = new ArrayList<String>();
		String[] arreglo = null;
		
		String indexBasePath = "c:\\indiceSinonimos";
		int maxFieldLength = 1000;
		MaxFieldLength mfl = new MaxFieldLength(maxFieldLength);
		Directory dir = null;
		try {		
			dir = FSDirectory.getDirectory(indexBasePath);					
			JFTSAnalyzer analyzer = new SpanishAnalyzer();
			IndexWriter iw = new IndexWriter(dir, analyzer, true, mfl);					


			is = new FileInputStream(f);
		    BufferedReader br = new BufferedReader(new InputStreamReader( is ));
		    
		    String linea = br.readLine();
		    while (linea != null){
		    	if(!linea.startsWith("-")){
		    		cantidad = _getCantidad(linea, synonymous);
			    	for(int i = 0; i < cantidad; i++){
			    		aux = br.readLine();
			    		arreglo = aux.split("\\|");
			    		_copyArrayToList(arreglo, synonymous);
			    	}
			    	if (synonymous.size() != 0){
			    		_indexSynonymous(iw, synonymous);
			    	}
			    	
			    	synonymous.clear();
		    	}
		    	
		    	
		    	System.out.println(linea);
		    	linea = br.readLine();
		    }
		    
		    br.close();
		    iw.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AnalyzerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	private static int _getCantidad(String linea, List<String> lista){
		int start = 0;
		int cantidad = 0;
		String palabra = null;
		start = linea.indexOf('|');
		if (start != -1){
			cantidad = Integer.parseInt(linea.substring(start + 1));
			palabra = linea.substring(0, start);
			lista.add(palabra);
		}
		return cantidad;
	}
	
	
	public static void _copyArrayToList(String[] arreglo, List<String> lista){
		for (int i = 1; i < arreglo.length; i++){
			lista.add(arreglo[i]);
		}
	}
	
	private static void _indexSynonymous(IndexWriter iw, List<String> synonymous){

		Document doc = new Document();
		doc.add(new Field("word", synonymous.get(0), Field.Store.YES, Field.Index.ANALYZED));
		
		for(int i = 1; i < synonymous.size(); i++){
			doc.add(new Field("syn", synonymous.get(i), Field.Store.YES, Field.Index.ANALYZED));
		}

		try {
			iw.addDocument(doc);
		}
		catch (CorruptIndexException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
